package MyDaily.Servlet.page;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import MyDaily.dao.daily.QuertDaily;
import MyDaily.pojo.Daily;
import MyDaily.pojo.Page;
import MyDaily.pojo.PageUtils;

/**
 * Servlet implementation class PageSer
 */
@WebServlet("/PageSer")
public class PageSer extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PageSer() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 从session中获取用户名
		HttpSession session = request.getSession();
		String name = (String) session.getAttribute("name");
		// 获取总数
		int pageSum;
		// 定义展示行数
		int pageSize = 10;
		// 获取页面输入的跳转
		String pageStr = request.getParameter("page");
			pageSum = QuertDaily.DailyCont(name);
			// 调用方法 计算分页参数
			List<Page> pageList = PageUtils.getPageList(pageSize, pageSum, pageStr);
			int startIndex = pageList.get(0).getStartIndex();
			// 调用查询方法
			List<Daily> dList = QuertDaily.queDailies(name, startIndex, pageSize);
			request.setAttribute("pageList", pageList);
			request.setAttribute("list", dList);
			request.getRequestDispatcher("mydaily.jsp").forward(request, response);
		}

}
